﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Dataglot
{
    
    public class EnumUtils
    {

        private EnumUtils()
        {
        }

        /// <summary>
        /// TODO: Throws "Object reference not set to an instance of an object"  error on a enum that doesn't exist
        /// Current solution is to quietly replace with "invalid enumeration"
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            if (fi == null)
                return "Null FieldInfo, Invalid enumeration";

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                                                typeof(DescriptionAttribute), false);
            if (attributes[0] == null)
                return "Invalid enumeration";

            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }


    }
    
}
